<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>按钮</button>

  <script>
    const elem = document.querySelector('button')

    elem.addEventListener('click', function () {
      console.log('onClick')
    })

    // 通过代码触发事件
    elem.click()
  </script>
</body>

</html>